<template>
  <div class="result">
    <header class="header">
      <h1>训练结果</h1>
    </header>
    
    <main class="main">
      <div class="stats-container">
        <div class="stat-card">
          <h3>完成题目数量</h3>
          <p class="stat-value">{{ statistics.completedQuestions }}</p>
        </div>
        
        <div class="stat-card">
          <h3>正确率</h3>
          <p class="stat-value">{{ statistics.accuracyRate.toFixed(1) }}%</p>
        </div>
        
        <div class="stat-card">
          <h3>平均用时</h3>
          <p class="stat-value">{{ (statistics.averageTimeSpent / 1000).toFixed(2) }}秒</p>
        </div>
      </div>
      
      <div class="performance-comment">
        <h2>{{ performanceComment.title }}</h2>
        <p>{{ performanceComment.message }}</p>
      </div>
      
      <div class="actions">
        <button class="action-btn primary" @click="restartTraining">
          再来一把
        </button>
        <button class="action-btn" @click="reconfigTraining">
          重新配置
        </button>
        <button class="action-btn" @click="selectOtherModule">
          选择其他训练
        </button>
      </div>
    </main>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { getSessionStatistics } from '@/services/statisticsService'

const router = useRouter()

const statistics = ref({
  completedQuestions: 0,
  accuracyRate: 0,
  averageTimeSpent: 0
})

const performanceComment = computed(() => {
  const accuracy = statistics.value.accuracyRate
  const avgTime = statistics.value.averageTimeSpent / 1000
  
  if (accuracy >= 90 && avgTime <= 3) {
    return {
      title: '优秀！',
      message: '你的表现非常出色！继续保持这样的专注和准确率。'
    }
  } else if (accuracy >= 80 && avgTime <= 4) {
    return {
      title: '良好！',
      message: '表现不错，再稍微提高一下准确率或速度就更棒了。'
    }
  } else if (accuracy >= 70 || avgTime <= 5) {
    return {
      title: '一般',
      message: '还有提升空间，多加练习会有明显进步。'
    }
  } else {
    return {
      title: '加油！',
      message: '不要气馁，学习是一个循序渐进的过程，继续努力！'
    }
  }
})

const restartTraining = () => {
  router.push('/training')
}

const reconfigTraining = () => {
  router.push('/config/ARITHMETIC_QUIZ')
}

const selectOtherModule = () => {
  router.push('/')
}

const loadStatistics = async (sessionId) => {
  try {
    const data = await getSessionStatistics(sessionId)
    statistics.value = {
      completedQuestions: data.completedQuestions || data.totalQuestions,
      accuracyRate: data.accuracyRate,
      averageTimeSpent: data.averageTimeSpent
    }
  } catch (error) {
    console.error('获取统计数据失败:', error)
    alert('获取统计数据失败: ' + (error.message || '请重试'))
  }
}

onMounted(() => {
  const urlParams = new URLSearchParams(window.location.search)
  const sessionId = urlParams.get('sessionId')
  
  if (sessionId) {
    loadStatistics(sessionId)
  }
})
</script>

<style scoped>
.result {
  min-height: 100vh;
  background-color: #f0f8ff;
}

.header {
  background-color: #4a90e2;
  color: white;
  padding: 1rem;
  text-align: center;
}

.header h1 {
  margin: 0;
  font-size: 2rem;
}

.main {
  padding: 2rem;
  max-width: 800px;
  margin: 0 auto;
}

.stats-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1.5rem;
  margin-bottom: 2rem;
}

.stat-card {
  background-color: white;
  border-radius: 10px;
  padding: 1.5rem;
  text-align: center;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.stat-card h3 {
  color: #666;
  margin-bottom: 1rem;
}

.stat-value {
  font-size: 2rem;
  font-weight: bold;
  color: #4a90e2;
  margin: 0;
}

.performance-comment {
  background-color: white;
  border-radius: 10px;
  padding: 2rem;
  text-align: center;
  margin-bottom: 2rem;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.performance-comment h2 {
  color: #4a90e2;
  margin-bottom: 1rem;
}

.performance-comment p {
  font-size: 1.1rem;
  color: #666;
  line-height: 1.6;
}

.actions {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.action-btn {
  padding: 1rem;
  font-size: 1.1rem;
  border: 2px solid #4a90e2;
  border-radius: 5px;
  background-color: white;
  color: #4a90e2;
  cursor: pointer;
  transition: all 0.2s;
}

.action-btn:hover {
  background-color: #4a90e2;
  color: white;
}

.action-btn.primary {
  background-color: #4a90e2;
  color: white;
}

.action-btn.primary:hover {
  background-color: #357ae8;
}

@media (min-width: 600px) {
  .actions {
    flex-direction: row;
    justify-content: center;
  }
  
  .action-btn {
    flex: 1;
    max-width: 200px;
  }
}
</style>